package com.erpanderp.wam.usm.model.service.leave;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.classic.Session;

import com.erpanderp.wam.cm.model.entity.EmpPersonalResume;
import com.erpanderp.wam.usm.model.entity.leave.LeaveBalance;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.HibernateUtil;

public class LeaveBalanceDAO {
	
	
	public Map<String, Float> getLeaveBalances(Long employeeId){
		Map<String, Float> leaveBalances = new HashMap<String, Float>();
		try{
			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			String queryString = "from LeaveBalance lb where lb.employee.employeeId = :employeeId";
			Query query = session.createQuery(queryString).setLong("employeeId", employeeId);
			List<LeaveBalance> leaveBalancesList = (List<LeaveBalance>) query.list();
			for(LeaveBalance balance : leaveBalancesList){
				if(balance.getLeaveType().getLeaveType().equals(CommonLookup.SICKLEAVE_TYPE)){
					leaveBalances.put(CommonLookup.SICKLEAVE_TYPE, balance.getBalance());
				}else if(balance.getLeaveType().getLeaveType().equals(CommonLookup.PRIVILIZEDLEAVE_TYPE)){
					leaveBalances.put(CommonLookup.PRIVILIZEDLEAVE_TYPE, balance.getBalance());
				}else if(balance.getLeaveType().getLeaveType().equals(CommonLookup.CASUALLEAVE_TYPE)){
					leaveBalances.put(CommonLookup.CASUALLEAVE_TYPE, balance.getBalance());
				}else if(balance.getLeaveType().getLeaveType().equals(CommonLookup.OPTIONALLEAVE_TYPE)){
					leaveBalances.put(CommonLookup.OPTIONALLEAVE_TYPE, balance.getBalance());
				}else if(balance.getLeaveType().getLeaveType().equals(CommonLookup.WORKFROMHOME_TYPE)){
					leaveBalances.put(CommonLookup.WORKFROMHOME_TYPE, balance.getBalance());
				}else if(balance.getLeaveType().getLeaveType().equals(CommonLookup.LOSS_OF_PAY_TYPE)){
					leaveBalances.put(CommonLookup.LOSS_OF_PAY_TYPE, balance.getBalance());
				}else{
					System.out.println("COMP OFF LEAVE BALANCE IS------------ ="+balance.getBalance());
					leaveBalances.put(CommonLookup.COMP_OF_TYPE, balance.getBalance());
				}
				
				
				
			}
			System.out.println("LEAVE  BALANCES SIZE IS:"+leaveBalances.size());
			return leaveBalances;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
		
	}
	public List<LeaveBalance> getLeaveBalancesList(Long employeeId){
		List<LeaveBalance> leaveBalancesList = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			String queryString = "from LeaveBalance lb where lb.employee.employeeId = :employeeId";
			Query query = session.createQuery(queryString).setLong("employeeId", employeeId);
			leaveBalancesList = (List<LeaveBalance>) query.list();
			return leaveBalancesList;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
	}
	
	public List<LeaveBalance> getLeaveBalances(){
		
		List<LeaveBalance> leaveBalancesList = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			String queryString = "from LeaveBalance lb";
			Query query = session.createQuery(queryString);
			leaveBalancesList = (List<LeaveBalance>) query.list();
			return leaveBalancesList;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
	}
	
	
	public LeaveBalance getActualLeaveBalanceByLeaveTypeIdAndEmployeeId(Long leaveTypeid , Long employeeId){
		System.out.println("IN GET ACTUAL BALANCE METHOD");
		LeaveBalance leaveBalance = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			
			String queryString = "from LeaveBalance l where l.employee.employeeId = :employeeId and l.leaveType.leaveTypeid = :leaveTypeid";
			Query query = session.createQuery(queryString).setLong("leaveTypeid", leaveTypeid).setLong("employeeId", employeeId);
			query.setCacheable(false);
			leaveBalance = (LeaveBalance) query.uniqueResult();
			session.close();
			return leaveBalance;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
		
		
		
	}
	
	
	
	public void updateLeaveBalance(LeaveBalance leaveBalance){
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			session.saveOrUpdate(leaveBalance);
			session.getTransaction().commit();
		}catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		
	}

}
